import React, { Component } from "react";
import story from "./story";
import Child from "./child";
export default class index extends Component {
  constructor() {
    super();
    this.state = {
      input: ""
    };
    // 监听
    story.subscribe(() => {
      // 这里的强制更新是为了让子组件能够获取到story里的新状态
      this.forceUpdate();
    });
  }
  // 提交
  handleBtn = () => {
    // 派发动作;
    story.dispatch({
      type: "setInput", // 必须的
      value: this.state.input
    });
  };
  // 渲染
  render() {
    return (
      <div>
        <Child />
        <input
          type="text"
          value={this.state.input}
          onChange={({ target }) => {
            this.setState({
              input: target.value
            });
          }}
        />
        <button onClick={this.handleBtn}> 提交</button>
      </div>
    );
  }
}
